global firstyear 1996
global lastyear 2010

local var1 l1avgret
local var2 l1lavgvol

use $correspath/corres_nc8$firstyear$lastyear.dta, clear // 19952010.dta, clear 
cap g nc8 = cn8
egen _=count(nc8), by(id_conc year) 
sort id_conc year
bys id_conc: egen nbm=max(_)
keep if year==2002 
tostring nc8, replace 
replace nc8="0"+nc8 if length(nc8)==7
g hs6_2002 =substr(nc8,1,6) if length(nc8)==8  

keep hs6 id_conc 
*destring hs6, replace
merge m:1 hs6_2002 using $rspath/rs_hs02_19962006.dta
keep if _m==3 
collapse (mean) rs_hs6_b , by(id_conc)
save temp, replace

use $countrypath/bbt_panel_data, clear 
g iso2="."
replace iso2="DE" if iso3=="DEU"
replace iso2="AT" if iso3=="AUT"
replace iso2="BE" if iso3=="BEL"
replace iso2="DK" if iso3=="DNK"
replace iso2="ES" if iso3=="ESP"
replace iso2="FI" if iso3=="FIN"
replace iso2="GB" if iso3=="GBR"
replace iso2="GR" if iso3=="GRC"
replace iso2="IT" if iso3=="ITA"
replace iso2="NL" if iso3=="NLD"
replace iso2="PT" if iso3=="PRT"
replace iso2="SE" if iso3=="SWE"

drop if iso2=="."
destring year, replace
keep if year>1995 & year<2011
egen t=group(year quarter)
*replace `var2'=exp(`var2') 

egen m=mean(`var1'), by(iso2)
egen sd=sd(`var1'), by(iso2)
g bbt1_shock= `var1'  <m - sd 
drop m sd 

egen m=median(`var2'), by(iso2)
egen sd=sd(`var2'), by(iso2)
g bbt2_shock= `var2' >m + sd 
drop m sd 

rename quarter trim 

pwcorr ydgdp cs_index_vol cs_index_ret bbt2 bbt1 
keep year trim bbt2 bbt1 iso2 cs_index_vol cs_index_ret l1avgcs_ret l1lavgcs_vol l1avgret l1lavgvol ydgdp 
merge 1:1 iso2 trim year using  $countrypath/uncertainty
keep if _m==3
drop _m
save temp_bbt, replace

local moment1 l1avgret
local moment2 l1lavgvol
use $firmpath/decompo_margin_$firstyear$lastyear, clear
destring year, replace
drop date
merge m:1 iso2 trim year using  temp_bbt
keep if _m==3 
drop _m 
merge m:1 id_conc using temp
keep if _m==3

g eta_uncert=`moment2'*rs
g eta_crisis=`moment1'*rs
g eta=rs 
drop t
egen c=group(iso2)

local y g_all 
reg `y' `moment2' eta_uncert `moment1' eta_crisis eta,   ro
outreg2  using  "$outputpath/table_OA12.xls" ,  label e(all) nocons bdec(2) symbol($^{***}$, $^{**}$, $^{*}$)    replace
foreach j in eta eta_uncert `moment1' `moment2' eta_crisis{
g coef_`y'`j'=_b[`j']
}

foreach y in entri exitc intens{ 
reg `y' `moment2' eta_uncert `moment1'  eta_crisis eta,  ro
outreg2  using  "$outputpath/table_OA12.xls" ,  label e(all) nocons bdec(2) symbol($^{***}$, $^{**}$, $^{*}$)    append
foreach j in eta eta_uncert `moment1' `moment2' eta_crisis{
g coef_`y'`j'=_b[`j']
}
}


erase temp_bbt.dta 
